# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html.
# For more examples on how to use CMake, see https://github.com/android/ndk-samples.

# 要求最低 CMake 版本
cmake_minimum_required(VERSION 3.22.1)

# 声明项目名称
project("playercore")

# 头文件路径：指向 media-core 的 include 文件夹
set(MEDIA_CORE_INCLUDE_DIR
        ${CMAKE_CURRENT_SOURCE_DIR}/third_party/media-core/include
        ${CMAKE_CURRENT_SOURCE_DIR}/third_party/media-core/include/mediacore/codec
        ${CMAKE_CURRENT_SOURCE_DIR}/third_party/media-core/include/mediacore/render
        ${CMAKE_CURRENT_SOURCE_DIR}/third_party/media-core/include/mediacore/utils
)
# 库文件路径：按 Android ABI 自动匹配（如 arm64-v8a/armeabi-v7a）
set(MEDIA_CORE_LIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/media-core/lib/${ANDROID_ABI})

# 自定义播放器源码
file(GLOB PLAYER_SRC ${CMAKE_CURRENT_SOURCE_DIR}/player/*.cpp)
# JNI 接口文件
file(GLOB JNI_SRC ${CMAKE_CURRENT_SOURCE_DIR}/jni/*.cpp)

# 创建共享库
add_library(${CMAKE_PROJECT_NAME} SHARED
        ${PLAYER_SRC}
        ${UTILS_SRC}
        ${JNI_SRC}
)

# 头文件包含路径
target_include_directories(${CMAKE_PROJECT_NAME}
        PUBLIC
        ${MEDIA_CORE_INCLUDE_DIR}
        ${CMAKE_CURRENT_SOURCE_DIR}/player
)

# 链接依赖库
target_link_libraries(${CMAKE_PROJECT_NAME}
        android
        log
        ${MEDIA_CORE_LIB_DIR}/libmediacore.so
        ${MEDIA_CORE_LIB_DIR}/libavutil.so
        ${MEDIA_CORE_LIB_DIR}/libavcodec.so
        ${MEDIA_CORE_LIB_DIR}/libavformat.so
        ${MEDIA_CORE_LIB_DIR}/libswscale.so
        ${MEDIA_CORE_LIB_DIR}/libswresample.so
        ${MEDIA_CORE_LIB_DIR}/libavfilter.so
)

# ----------------------- 模式区分与优化配置 -----------------------
# Debug 模式配置：保留调试信息
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -g -ggdb")
# 给 Debug 模式添加链接选项
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} -rdynamic -fuse-ld=lld -Wl,-z,max-page-size=16384 -Wl,--exclude-libs,ALL")

# Release 模式配置：开启优化，去除调试信息
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2 -DNDEBUG")
# 给 Release 模式添加链接选项
set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} -rdynamic -fuse-ld=lld -Wl,-z,max-page-size=16384 -Wl,--exclude-libs,ALL")